package com.example.publishDemo.Service.Impl;

import com.example.publishDemo.Mapper.CommentMapper;
import com.example.publishDemo.Model.Comment;
import com.example.publishDemo.Service.CommentService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Resource
    CommentMapper commentMapper;

    @Override
    public int save(Comment comment) {
        return commentMapper.save(comment);
    }

    @Override
    public List<Comment> articleComment(int a_id) {
        return commentMapper.articleComment(a_id);
    }

    @Override
    public int delComment(int c_id, int r_id, int p_id) {

        if (p_id == 0) {    //p_id=0表示它是根节点，子节点也需要删除
            int i = delCommentChild(c_id, r_id, p_id);
            int i1 = delCommentRoot(r_id, c_id);
            return i + i1;
        }else {
            return delCommentChild(c_id,r_id,p_id);
        }
    }

    private int delCommentRoot(int r_id,int c_id) {     //只需要知道这篇文章的id(r_id)和根节点的id(c_id)就可以把该文评论下的所有子节点删除
        return commentMapper.delCommentRoot(r_id,c_id);
    }

    private int delCommentChild(int c_id, int r_id, int p_id) {     //删除一个评论
        return commentMapper.delCommentChild(c_id,r_id,p_id);
    }
}
